using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Hardware;

namespace MicroSnake
{
    class LanguageScreen : Screen
    {
        public LanguageScreen()
        {
            InitializeComponent();
        }

        private Text txtTitle;
        private ListBox selectionList;
        private ListBoxImageTextMenuItem italianMenuItem;
        private ListBoxImageTextMenuItem englishMenuItem;
        
        /// <summary>
        /// Built the menu graphical user interface
        /// </summary>
        private void InitializeComponent()
        {
            selectionList = new ListBox();
            selectionList.HorizontalAlignment = HorizontalAlignment.Center;

            // Menu Title                
            txtTitle = new Text()
            {
                Font = Resources.GetFont(Resources.FontResources.NinaB),
                ForeColor = MicroSnakeColors.MenuTitleForeColor,
                HorizontalAlignment = HorizontalAlignment.Center,
                TextAlignment = TextAlignment.Center,
                TextContent = Resources.GetString(Resources.StringResources.LanguageScreenTitle),
                VerticalAlignment = VerticalAlignment.Center,
            };
            txtTitle.SetMargin(10);            

            selectionList.Items.Add(new ListBoxMenuTitle(txtTitle));

            // Separator
            selectionList.Items.Add(new ListBoxSpaceItem());

            // Menu Items
            italianMenuItem = ListBoxImageTextMenuItem.Make(Resources.BitmapResources.italian, Resources.StringResources.LanguageScreenItalian);
            englishMenuItem = ListBoxImageTextMenuItem.Make(Resources.BitmapResources.english, Resources.StringResources.LanguageScreenEnglish);

            selectionList.Items.Add(englishMenuItem);
            selectionList.Items.Add(italianMenuItem);

            // TODO: load the language settings from flash memory
            selectionList.SelectedItem = englishMenuItem;

            // Add event handler
            selectionList.AddHandler(Buttons.ButtonDownEvent, new ButtonEventHandler(menuList_ButtonDown), false);

            this.Children.Add(selectionList);
        }

        /*********************************************************************/
        /*                       Event Handling                              */
        /*********************************************************************/

        void menuList_ButtonDown(object sender, ButtonEventArgs e)
        {
            if (e.Button == Button.VK_SELECT)
            {
                if (selectionList.SelectedItem == englishMenuItem)
                {
                    Util.SetCulture("en");
                    ScreenManager.Instance.ShowOptionsScreen();
                }
                else if (selectionList.SelectedItem == italianMenuItem)
                {
                    Util.SetCulture("it");
                    ScreenManager.Instance.ShowOptionsScreen();
                }
            }
        }

        /// <summary>
        /// Set the focus to the internal menuList
        /// </summary>
        public override void Focus()
        {
            Buttons.Focus(selectionList);
        }
    }
}
